﻿namespace Memento
{
    internal class Gamer
    {
        private int money;
        private List<string> fruits = new();
        private Random random = new();

        private static string[] fruitsName =
        {
            "苹果","葡萄","香蕉","橘子"
        };

        public Gamer(int money)
        {
            this.money = money;
        }

        public int GetMoney()
        {
            return this.money;
        }

        public void Bet()
        {
            var dice = this.random.Next(6) + 1;
            if (dice == 1)
            {
                money += 100;
                Console.WriteLine("所持金钱增加了。");
            }
            else if (dice == 2)
            {
                money /= 2;
                Console.WriteLine("所持金钱减半了。");
            }
            else if (dice == 6)
            {
                var f = this.GetFruit();
                Console.WriteLine($"获得水果（{f}）。");
                fruits.Add(f);
            }
            else
            {
                Console.WriteLine("什么都没有发生。");
            }
        }

        private string GetFruit()
        {
            var prefix = "";
            if (random.Next(2) == 1)
            {
                prefix = "好吃的";
            }

            return prefix + fruitsName[random.Next(fruitsName.Length)];
        }

        public override string ToString()
        {
            return $"[money = {money}, fruits = {string.Join(',', fruits)}]";
        }

        public Memento CreateMemento()
        {
            var m = new Memento(this.money);
            foreach (var f in fruits)
            {
                if (f.StartsWith("好吃的"))
                {
                    m.AddFruit(f);
                }
            }
            return m;
        }

        public void RestoreMemento(Memento memento)
        {
            this.money = memento.GetMoney();
            this.fruits = memento.GetFruits();
        }
    }
}
